<?php

/**
 * data.php - simple data manager
 *
 * This is a very simple data management class that allows the code to
 * manipulate data as hash arrays. The details of where the data gets stored and
 * how the data gets cached is defined in the Frameworky configuration.
 */
class Data {
	protected $db = null;

	public function __construct($host, $name, $user, $pass){
		self::$db = new Db($host, $name, $user, $pass);
	}

	/**
	 * get data
	 *
	 * @param array data object
	 * @return array data object
	 */
	public function get_data($data){
		$sql = null;
		$result = array();

		// setup initial sql select
		if ($data['data_id'] != null) $sql = "select data_id, model_id, data_key, version_id from data where data_id = '{$data['data_id']}'";
		else if ($data['data_key'] != null) $sql = "select data_id, model_id, data_key, version_id from data where data_key = '{$data['data_key']}'";
		else return(array_merge($data, array('error' => 'get_data: Unique Identifier Required')));

		// consider versioning requests
		if ($data['version_id'] != null) $sql .= " and version_id <= '{$data['version_id']}'";

		// get the data
		$result = self::$db->query($sql);
		if (!$result) return(array_merge($data, array('error' => "get_data: Invalid query: {self::$db->error()}")));
		else {
			$data = array_merge($data, self::$db->fetch_assoc($result));

			// get the model
			$model = self::get_model(array('model_id' => $data['model_id']));
			if ($model['error'] != null) {
				return(array_merge($data, array('error' => $model['error']), $model));
			} else {
				$data = array_merge($data, array('model' => $model));

				// get the fields
				$result = self::get_fields($data['']);
			}
		}
	}

	/**
	 * get model
	 *
	 * @param array model object
	 * @return array model object
	 */
	public function get_model($model){
		$sql = null;
		$result = array();

		// setup initial sql select
		if ($model['model_id'] != null) $sql = "select model_id, model_key, version_id from model where model_id = '{$model['model_id']}'";
		else if ($model['model_key'] != null) $sql = "select model_id, model_key, version_id from model where model_key = '{$model['model_key']}'";
		else return(array_merge($model, array('error' => 'get_model: Unique identifier Required')));

		// consider versioning requests
		if ($model['version_id'] != null) $sql .= " and version_id <= '{$data['version_id']}'";

		// get the model
		$result = self::$db->query($sql);
		if (!$result) return(array_merge($model, array('error' => "get_data: Invalid query: {self::$db->error()}")));
		else return(array_merge($model, self::$db->fetch_assoc($result)));
	}

	/**
	 * get fields
	 *
	 * @param array

	/**
	 * get field
	 *
	 * @param array field object
	 * @return array field object
	 */
	public function get_field($field){
		$sql = null;
		$result = array();

		// setup initial sql select
		if ($field['field_id'] != null) $sql = "select field_id, field_key, field_description, model_id, type, sequence, version_id from field where field_id = '{$field['field_id']}'";
		else if ($field['field_key'] != null) $sql = "select field_id, field_key, field_description, model_id, type, sequence, version_id from field where field_key = '{$field['field_key']}'";
		else return(array_merge($field, array('error' => 'get_field: Unique identifier Required')));

		// consider versioning requests
		if ($field['version_id'] != null) $sql .= " and version_id <= '{$data['version_id']}'";
		$sql .= ' order by sequence';

		// get the field
		$result = self::$db->query($sql);
		if (!$result) return(array_merge($field, array('error' => "get_field: Invalid query: {self::$db->error()}")));
		else return(array_merge($field, self::$db->fetch_assoc($result)));
	}
}

?>
